package br.com.poliwar.view {
import br.com.poliwar.model.vo.LoginVO;
import br.com.poliwar.model.vo.UsuarioVO;
import br.com.poliwar.view.events.TelaCadastroEvent;
	import br.com.poliwar.view.events.TelaLoginEvent;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author Ricardo Teixeira
	 */
	public class TelaLogin extends LoginSWC{
		
		public function TelaLogin () { 			
			this.campoSenha.displayAsPassword = true;
			this.btEntrar.addEventListener (MouseEvent.CLICK, onClickEntrar);
			this.btNovo.addEventListener (MouseEvent.CLICK, onClickNovo);
		}
		
		private function validarEmail (email:String):Boolean {
			var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
			return emailExpression.test(email);
		}
		
		private function onClickEntrar(e:MouseEvent):void {
			var loginVO:LoginVO = new LoginVO ();
			loginVO.login = this.campoLogin.text;
			loginVO.senha = this.campoSenha.text;
			
			var evento:TelaLoginEvent;
			if (this.validarEmail (this.campoLogin.text)) {
				evento = new TelaLoginEvent (TelaLoginEvent.ENTRAR, loginVO);
			} else {
				evento = new TelaLoginEvent (TelaLoginEvent.DADOS_INVALIDOS, loginVO);
			}
			this.dispatchEvent (evento);
		}
		
		private function onClickNovo(e:MouseEvent):void {
			var evento:TelaLoginEvent = new TelaLoginEvent (TelaLoginEvent.NOVO);
			this.dispatchEvent (evento);
		}
		
	}

}